package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiyun.comfig.R;
import com.jiyun.common.result.Result;
import com.jiyun.entity.Technician;
import com.jiyun.entity.User;

import com.jiyun.feign.AdminFeignClient;
import com.jiyun.mapper.UserMapper;
import com.jiyun.model.pojo.RechargeDetails;
import com.jiyun.service.IRechargeDetailsService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.jiyun.service.UserService;

import java.math.BigDecimal;
import java.util.List;


/**
 * (User)表控制层
 *
 * @author LYT
 * @since 2025-01-17 18:29:06
 */

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("login")
    public R login(@RequestBody User user, HttpSession session) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User userDB = userService.login(user);
        System.out.println(userDB);
        if (userDB == null) {
            return R.error("账号或密码错误");
        }
        session.setAttribute("user", userDB);
        return R.ok().put("user", userDB);
    }

    /**
     * 发送验证码
     * @param phone
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping("sendCode/{phone}")
    public R sendCode(@PathVariable String phone, String code) throws Exception {
        return userService.sendCode(phone, code);
    }

    /**
     * 注册
     * @param user
     */
    @RequestMapping("register")
    public void register(@RequestBody User user) {
        userService.register(user);
    }

    /**
     * 检查手机号是否存在
     * @param user
     * @return
     */
    @RequestMapping("checkPhone")
    public R checkPhone(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, user.getPhone());
        User phoneDb = userService.checkPhone(user);
        if (phoneDb == null){
            return R.error("手机号不存在！！！");
        }
        return R.ok().put("phone",phoneDb);
    }

    /**
     * 忘记密码
     * @param user
     */
    @RequestMapping("/forget")
    public void forget(@RequestBody User user) {
        userService.updatePassword(user);
    }

    /**
     * 查询所有手机号
     * @return
     */
    @RequestMapping("/findAllPhone")
    public List<User> findAllPhone() {
        return userService.findAllPhone();
    }

    /**
     * 接口：修改
     * @param user 要修改的数据
     * @return Result 响应给前端
     * author cui
     */
    @PutMapping("update")
    @ApiOperation("修改")
    public Result updateById(@RequestBody User user) {
        System.out.println(user);
        userService.updateById(user);
        return Result.ok();
    }

    /**
     * 获取优惠券个数
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/getCountCoupon/{id}")
    @ApiOperation("查询当前账号的优惠卷个数")
    public Result getCountCoupon(@PathVariable Long id) {
        int list = userService.getCountCoupon(id);
        return Result.ok(list);
    }
    @Autowired
    private IRechargeDetailsService rechargeDetailsService;
    @PostMapping("/updatMoney/{id}/{money}")
    @ApiOperation("充值金额")
    public Result updateMoney(@PathVariable Integer id,@PathVariable Integer money) {
        Integer i = userService.updateMonuey(id, money);
        RechargeDetails rechargeDetails = new RechargeDetails();
        rechargeDetails.setType("充值");
        rechargeDetails.setAmount(BigDecimal.valueOf(money));
        rechargeDetails.setTime(new java.util.Date());
        rechargeDetails.setBalance(BigDecimal.valueOf(userService.getById(id).getAccountBalance()));
        rechargeDetails.setUserName(userService.getById(id).getUsername());
        rechargeDetailsService.save(rechargeDetails);
        return Result.ok(i);
    }
    @PostMapping("/getMoney/{username}")
    @ApiOperation("充值明细")
    public Result getMoney(@PathVariable String username) {
        List<RechargeDetails> list = rechargeDetailsService.list(new LambdaQueryWrapper<RechargeDetails>().eq(RechargeDetails::getUserName,username));
        return Result.ok(list);
    }



    /**
     * 查询当前账号的收藏个数
     *
     * @param id 用户ID
     * @return 收藏个数
     */
    @GetMapping("/getFavoriteCount/{id}")
    @ApiOperation("查询当前账号的收藏个数")
    public Result getFavoriteCount(@PathVariable Long id) {
        int count = userService.getFavoriteCount(id);
        return Result.ok(count);
    }

    /**
     * 查询当前账号收藏的技师
     *
     * @param id 用户ID
     * @return 技师列表
     */
    @GetMapping("/getFavoriteTechnicians/{id}")
    @ApiOperation("查询当前账号收藏的技师")
    public Result getFavoriteTechnicians(@PathVariable Long id) {
        List<Technician> technicians = userService.getFavoriteTechnicians(id);
        return Result.ok(technicians);
    }
    @GetMapping("/findAllUser")
    @ApiOperation("查询当前存储的所有账号")
    public com.jiyun.R.R findAllUser() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getIsDelete,0);
        List<User> users=userService.list(qw);
        return com.jiyun.R.R.ok().data("user",users);
    }



    @Resource
    private AdminFeignClient adminFeignClient;


    @GetMapping("deleteById")
    @ApiOperation("注销账号")

    public com.jiyun.R.R deleteById(@RequestParam("id") Integer id) {

        System.out.println(id);

//        //将address表中外键与user表中id相同的删除
//        adminFeignClient.deleteAddressById(id);
//
//        //将feedback表中外键与user表中id相同的删除
//        adminFeignClient.deleteById(id);
//
//        //将账号从库中删除
//        userService.removeById(id);
        //逻辑删除账号
        User user = userMapper.selectById(id);
        user.setIsDelete(1);
        userMapper.updateById(user);


        return com.jiyun.R.R.ok().message("账号已注销，请重新登录");
    }
}

